﻿using Transformable_Engine_v2.Engine.Animation.ColorAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.ObjectInterfaces
{
    public abstract class BasePickableItem : Container, IOrderable, IPickable
    {
        private readonly TE_Rectangle _boundingBox;

        protected ImageContainer _item;
        protected ImageContainer _outline;

        private Timer _reappearTimer;

        protected abstract int ReappearTime { get; }

        protected BasePickableItem()
        {
            _boundingBox = new TE_Rectangle(0, 0, 1, 1).ToGridBound();

            _reappearTimer = new Timer(ReappearTime, false, Reappear);

            AddTimers(_reappearTimer);
        }

        public override void Draw()
        {
            if (_item.Visible)
            {
                TE_Rectangle playerBoundingBox = Playscreen.CurrentlyDrawnPlayer.ItemBoundingBox; 
                if (playerBoundingBox != null && CollisionHelper.Intersects(playerBoundingBox, GetBoundingBoxPickup()) && Playscreen.CurrentlyDrawnPlayer.Alive)
                {
                    _outline.Visible = true;
                    _outline.Color = Playscreen.CurrentlyDrawnPlayer.GetTeam().GetColor();
                }
                else
                {
                    _outline.Visible = false;
                }
            }
            else
            {
                _outline.Visible = false;
            }

            base.Draw();
        }

        public TE_Rectangle GetBoundingBoxPickup()
        {
            return GetBoundingBoxCollision();
        }

        public TE_Rectangle GetBoundingBoxCollision()
        {
            return _boundingBox + Position;
        }

        public float GetOrderingFactor()
        {
            return Position.Y;
        }

        public virtual IThrowable Pickup()
        {
            _item.Visible = false;
            _reappearTimer.Reset();

            return GetPickupItem();
        }

        public virtual bool IsPickable 
        { 
            get
            {
                return _item.Visible;
            }
        }

        protected virtual void Reappear()
        {
            _item.Visible = true;
            _item.Color = Color.Transparent;
            _item.AddAnimations(new ColorAnimation(new LinearEasing(), Color.White, 500, false, 0));
        }

        protected abstract IThrowable GetPickupItem();
    }
}
